﻿using AntDesign;
using Microsoft.AspNetCore.Components;
using System.Collections.Generic;
using System.Threading.Tasks;
using YwhWeb.Models;
using YwhWeb.Services;

namespace YwhWeb.Pages.Daily
{
    public partial class PhaseView
    {
        private CurrentUser _currentUser = new CurrentUser();

        [Inject] protected IUserService UserService { get; set; }

        private void HandleFinish()
        {
        }

        protected override async Task OnInitializedAsync()
        {
            await base.OnInitializedAsync();
            this.phaseRsp = YwhCore.PhaseService.List(_pageIndex, _pageSize);
            _currentUser = await UserService.GetCurrentUserAsync();
        }

        public YwhLib.QueryRsp<YwhLib.Phase> phaseRsp = new YwhLib.QueryRsp<YwhLib.Phase>();

        IEnumerable<YwhLib.Phase> selectedRows;
        ITable table;

        int _pageIndex = 1;
        int _pageSize = 10;
        int _total = 0;

        bool drawerVisible = false;

        void NewDrawer()
        {
            this.editPhase = new YwhLib.Phase();
            this.drawerVisible = true;
        }

        public async Task OnChange(AntDesign.TableModels.QueryModel<YwhLib.Phase> queryModel)
        {
            //Console.WriteLine(JsonSerializer.Serialize(queryModel));
        }

        private YwhLib.Phase editPhase = new YwhLib.Phase();

        private async Task DeletePhase()
        {
            YwhCore.PhaseService.Delete(editPhase);
            this.phaseRsp = YwhCore.PhaseService.List(_pageIndex, _pageSize);
            base.StateHasChanged();
        }

        private async Task UpdatePhase()
        {
            this.drawerVisible = true;
            YwhCore.PhaseService.Update(editPhase);
            this.phaseRsp = YwhCore.PhaseService.List(_pageIndex, _pageSize);
            base.StateHasChanged();
        }

        private async Task SavePhase()
        {
            YwhCore.PhaseService.Save(editPhase);
            this.drawerVisible = false;
            this.phaseRsp = YwhCore.PhaseService.List(_pageIndex, _pageSize);
            base.StateHasChanged();
        }
    }
}
